import { getTimeAtom } from '@/utils/func'
import store from '@/store'
import i18n from '@/i18n'

export default {
  install (Vue) {
    // 转换状态
    Vue.filter('userStatus', val => {
      //  数据可以直接从i18n的数据文件中取出，但是数据已经绑到了vue上面，不如直接使用
      const data = {
        en: {
          0: 'no',
          1: 'yes',
        },
        zh: {
          0: '否',
          1: '是',
        },
      }
      const lang = store.state.language
      const a = data[lang][val]
      return a
    })
    Vue.filter('userStatus1', val => {
      return i18n.t('user.statusList')[val]
    })
    Vue.filter('test', val => {
      console.log('test过滤器被调用了')
      return 'test_' + val
    })

    // 格式化时间
    Vue.filter('formatTime', (val, formate = 'second') => {
      if (!val) return ''
      const { year, month, date, hours, minutes, seconds } = getTimeAtom(val)
      const separator = store.state.language === 'zh' ? '-' : '/'
      if (formate === 'month') {
        return [year, month].join(separator)
      } else if (formate === 'date') {
        return [year, month, date].join(separator)
      } else if (formate === 'hour') {
        return [year, month, date].join(separator) + ' ' + hours
      } else if (formate === 'minute') {
        return [year, month, date].join(separator) + ' ' + [hours, minutes].join(':')
      } else if (formate === 'second') {
        return [year, month, date].join(separator) + ' ' + [hours, minutes, seconds].join(':')
      }
      return '---'
    })
  },
}
